<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>注册页面</title>
    <style>
        body {
            background: url("img0/1.jpg");
        }

        .rg_layout {
            width: 850px;
            height: 600px;
            border: 8px solid #EEEEEE;
            background-color: white;
            transform: translate(50%,40%);
        }

        .rg_left {
            float: left;
            margin: 15px;
        }
        .rg_center {
            float: left;
            margin-top: 5%;
        }
        .rg_right {
            float: right;
            margin: 15px;
        }

        .rg_left > p:first-child {
            color:#FFD026;
            font-size: 20px;
        }

        .rg_left > p:last-child {
            color:#A6A6A6;
            font-size: 20px;
        }

        .rg_right > p:first-child {
            font-size: 15px;

        }
        .rg_right p a {
            color:pink;
        }

        .td_left {
            width: 100px;
            height: 45px;
            text-align: right;
        }
        .td_right {
            padding-left: 50px ;
        }

        #username, #password, #email, #name, #tel, #birthday, #checkcode,#confirm_password {
            width: 251px;
            height: 32px;
            border: 1px solid #A6A6A6;
            border-radius: 5px;
            padding-left: 10px;
        }

        #checkcode {
            width: 110px;
        }

        #img_check {
            height: 32px;
            vertical-align: middle;
        }

        #btn_sub {
            width: 150px;
            height: 40px;
            background-color: #FFD026;
            border: 1px solid #FFD026 ;
        }

        .error {
            color:red;
        }
        #td_sub {
            padding-left: 150px;
        }
    </style>
    <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script>
        $(document).ready(function(){
            $("#form").submit(function(){
                return checkUsername() && checkPassword();
            });
            $("#username").blur(checkUsername);
            $("#password").blur(checkPassword);
            $("#tel").blur(checkPhone);
            $("#confirm_password").blur(checkConfirmPassword)
        });

        function checkUsername(){
            var username = $("#username").val();
            var reg_username = /^\w{6,12}$/;
            var flag = reg_username.test(username);
            var s_username = $("#s_username");
            if(flag){
                s_username.html("<img width='35' height='25' src='img0/gou.png'/>");
            }else{
                s_username.html("用户名格式有误");
            }
            return flag;
        }

        function checkPassword(){
            var password = $("#password").val();
            var reg_password = /^\w{8,20}$/;
            var flag = reg_password.test(password);
            var s_password = $("#s_password");
            if(flag){
                s_password.html("<img width='35' height='25' src='img0/gou.png'/>");
            }else{
                s_password.html("密码长度在8~20位之间");
            }
            return flag;
        }

        function checkConfirmPassword(){
            var password = $("#password").val();
            var confirm_password = $("#confirm_password").val();
            var flag = (confirm_password === password);
            var s_confirm_password = $("#s_confirm_password");
            if(flag){
                s_confirm_password.html("<img width='35' height='25' src='img0/gou.png'/>");
            }else{
                s_confirm_password.html("两次输入的密码不一致,请重新输入");
            }
            return flag;
        }

        function checkPhone(){
            var phone = $("#tel").val();
            var reg_phone = /^\d{11}$/;
            var flag = reg_phone.test(phone);
            var s_phone = $("#s_phone");
            if(flag){
                s_phone.html("<img width='35' height='25' src='img0/gou.png'/>");
            }else{
                s_phone.html("手机号必须为11位数字");
            }
            return flag;
        }

        $(document).ready(function(){
            $("#form").submit(function(){
                // 进行输入框有效性验证
                if(checkUsername() && checkPassword() && checkPhone()){
                    // 发送注册请求
                    $.ajax({
                        url: "register.php", // 服务器端处理注册请求的地址
                        type: "GET",
                        data: $("#form").serialize(),
                        success: function(data){
                            // 注册成功
                            $("#form")[0].reset(); // 清空表单
                            alert("注册成功！");
                        },
                        error: function(){
                            // 注册失败
                            alert("注册失败，请稍后再试！");
                        }
                    });
                }
                return false; // 阻止表单提交
            });
        });

    </script>
</head>
<body>

<div class="rg_layout">

    <div class="rg_left">
        <p>新用户注册</p>
        <p>USER REGISTER</p>
    </div>

    <div class="rg_center">
        <div class="rg_form">
            <form action="#" id="form" method="get">
                <table>
                    <tr>
                        <td class="td_left"><label for="username">用户名</label></td>
                        <td class="td_right">
                            <input type="text" name="username" id="username" placeholder="请输入用户名">
                            <span id="s_username" class="error"></span>
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="password">密码</label></td>
                        <td class="td_right">
                            <input type="password" name="password" id="password" placeholder="请输入密码">
                            <span id="s_password" class="error"></span>
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="confirm_password">确认密码</label></td>
                        <td class="td_right">
                            <input type="password" name="confirm_password" id="confirm_password" placeholder="请再次输入密码">
                            <span id="s_confirm_password" class="error"></span>
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="email">Email</label></td>
                        <td class="td_right"><input type="email" name="email" id="email" placeholder="请输入邮箱"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="name">姓名</label></td>
                        <td class="td_right"><input type="text" name="name" id="name" placeholder="请输入姓名"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="tel">手机号</label></td>
                        <td class="td_right">
                            <input type="text" name="tel" id="tel" placeholder="请输入手机号">
                            <span id="s_phone" class="error"></span>
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label>性别</label></td>
                        <td class="td_right">
                            <input type="radio" name="gender" value="male" checked> 男
                            <input type="radio" name="gender" value="female"> 女
                        </td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="birthday">出生日期</label></td>
                        <td class="td_right"><input type="date" name="birthday" id="birthday" placeholder="请输入出生日期"></td>
                    </tr>

                    <tr>
                        <td class="td_left"><label for="checkcode" >验证码</label></td>
                        <td class="td_right"><input type="text" name="checkcode" id="checkcode" placeholder="请输入验证码">
                            <img id="img_check" src="img0/yz.png">
                        </td>
                    </tr>


                    <tr>
                        <td colspan="2" id="td_sub"><input type="submit" id="btn_sub" value="注册"></td>
                    </tr>
                </table>

            </form>
        </div>
    </div>

    <div class="rg_right">
        <p>已有账号?<a href="#">立即登录</a></p>
    </div>

</div>

</body>
</html>
